package com.use.utils.db;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.Page;
import cn.hutool.db.PageResult;
import cn.hutool.db.ds.DSFactory;

import java.sql.SQLException;
import java.util.List;

public class Test01 {
    public static void main(String[] args) throws SQLException {

        /**
         * SQL脚本
         * create table hutool.user
         * (
         *     id   int auto_increment
         *         primary key,
         *     name varchar(24) null,
         *     age  int         null
         * );
         */
        //创建数据源hutool1
        DSFactory.get("hutool1");
        Db hutools = Db.use("hutool1");
        insert(hutools);
//        delete(hutools);
//        update(hutools);
//        select(hutools);
//        tx(hutools);
        page(hutools);

    }

    public static void insert(Db hutools ) throws SQLException {
        hutools.insert(
                Entity.create("user")
                        .set("name", "unitTestUser")
                        .set("age", 66)
        );
        hutools.insert(
                Entity.create("user")
                        .set("name", "unitTestUser2")
                        .set("age", 66)
        );
    }

    public static void delete(Db hutools ) throws SQLException {
        hutools.del(
                Entity.create("user").set("name", "unitTestUser2")//where条件
        );


    }

    public static void update(Db hutools ) throws SQLException {
        hutools.update(
                Entity.create().set("age", 88), //修改的数据
                Entity.create("user").set("name", "unitTestUser") //where条件
        );

    }

    public static void select(Db hutools ) throws SQLException {
        //user为表名
//        Db.use().findAll("user");
        List<Entity> all =hutools.findAll(Entity.create("user").set("name", "unitTestUser"));
        all.stream().forEach(data-> System.out.println(data));
        all.stream().forEach(data-> System.out.println(data.get("name")));

    }

    //事务
    public static void tx(Db hutools ) throws SQLException {
        hutools.tx(db -> {
            db.insert(Entity.create("user").set("name", "unitTestUser2").set("age",79));
            db.update(Entity.create().set("name", "unitTestUser2unitTestUser2unitTestUser2unitTestUser2"), Entity.create("user").set("age", 79));
        });


    }

    public static void page(Db hutools ) throws SQLException {
        //Page对象通过传入页码和每页条目数达到分页目的
        PageResult<Entity> result = hutools.page(Entity.create("user").set("age", "> 0"),
                new Page(1, 5));
        System.out.println(result);
        if(CollectionUtil.isNotEmpty(result)){
            for (Entity entity : result) {
                System.out.println(entity.get("name"));
                System.out.println(entity.get("age"));
                System.out.println(entity.get("id"));
                System.out.println("-------------");
            }
        }

    }
}
